package com.mediacloud.app.newsmodule.fragment.baoliao.holder;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.mediacloud.app.model.news.ArticleItem;
import com.mediacloud.app.model.news.CatalogItem;
import com.mediacloud.app.model.news.kt.TypeX;
import com.mediacloud.app.model.utils.DefaultBgUtil;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapter;
import com.mediacloud.app.newsmodule.fragment.baoliao.adapter.BaoLiaoNavListAdapter;
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoListItem;
import com.mediacloud.app.newsmodule.model.baoliao.list.list.BaoNiaoMedia;
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.mediacloud.app.reslib.enums.ListStyle;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 资讯新闻样式的爆料item显示
 * */
public class BaoLiaoNavListNewsItemVideoHolder extends BaoLiaoNavListPureTxtHolder implements BaoLiaoNavListAdapter.IBaoLiaoListHolder, BaseRecyclerAdapter.ItemClickListener {
    RelativeLayout relativeContent;
    private ImageView imageNews;

    private String TAG = BaoLiaoNavListNewsItemVideoHolder.class.getSimpleName();

    public BaoLiaoNavListNewsItemVideoHolder(View view) {
        super(view);
        relativeContent = view.findViewById(R.id.relative_content);
        imageNews = view.findViewById(R.id.image_bao_liao_news_item);
    }

    @Override
    public void updateData(BaoNiaoListItem item,int position) {//不要直接调用supper,很多使用的View不一样。会空指针
//        GlideUtils.loadUrl(item.getLogo(), imageNews, null, defaultLoading, false,false);
        baoniaoCategroy.setTextColor(DefaultBgUtil.getTintColor(baoniaoCategroy.getContext()));
        if(TextUtils.isEmpty(item.getLogo())){
            imageNews.setVisibility(View.GONE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeContent.getLayoutParams();
            params.leftMargin = 0;
            relativeContent.setLayoutParams(params);
        }else {
            boolean isLeft = ListStyle.TotoalStyle.LEFT_IMAGE.equals(AppFactoryGlobalConfig.getAppServerConfigInfo(imageNews.getContext()).getContent_list());
            if(isLeft){
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeContent.getLayoutParams();
                params.leftMargin = relativeContent.getContext().getResources().getDimensionPixelSize(R.dimen.dimen22);
                relativeContent.setLayoutParams(params);
            }else {//右图
                RelativeLayout.LayoutParams imgLayoutParams = (RelativeLayout.LayoutParams) imageNews.getLayoutParams();
                imgLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
                imageNews.setLayoutParams(imgLayoutParams);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) relativeContent.getLayoutParams();
                params.removeRule(RelativeLayout.RIGHT_OF);
                params.addRule(RelativeLayout.LEFT_OF,R.id.image_bao_liao_news_item);
                params.rightMargin = relativeContent.getContext().getResources().getDimensionPixelSize(R.dimen.dimen22);
                relativeContent.setLayoutParams(params);
            }
            imageNews.setVisibility(View.VISIBLE);
            Glide.with(imageNews.getContext())
                    .load(item.getLogo())
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(AppFactoryGlobalConfig.getAppServerConfigInfo(imageNews.getContext()).isRound()?12:1)).placeholder(defaultLoading))
                    .into(imageNews);
        }
  /*      reflect.set("mIsExpanded", false);
        reflect.set("mShowWidth", 0);*/
//        titleTv.setText(Html.fromHtml(parseSearchKwData(item.title, keyWord, AppFactoryGlobalConfig.getAppServerConfigInfo(mContext).getColor())));
//        if (TextUtils.isEmpty(item.getTitle())) {
//            baoniaoTitle.setText("");
//        } else {
//            baoniaoTitle.setText(item.getTitle());
//        }
//        String lanmu = item.getTitle();
//
//        baoniaoCategroy.setVisibility(View.GONE);
//        if (!TextUtils.isEmpty(item.getCatalogName())) {
//            lanmu = item.getCatalogName() + "|"+item.getTitle();
//            SpannableString spannableString = new SpannableString(lanmu);
//            spannableString.setSpan(new ForegroundColorSpan(DefaultBgUtil.getTintColor(baoniaoCategroy.getContext())), 0, (item.getCatalogName() + "|").length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            spannableString.setSpan(new StyleSpan(Typeface.BOLD),0, (item.getCatalogName() + "|").length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            baoniaoTitle.setText(spannableString);
//        }else {
//            baoniaoTitle.setText(lanmu);
//        }


        if (TextUtils.isEmpty(item.getTitle())) {
            baoniaoTitle.setText("");
        } else {
            baoniaoTitle.setText("| "+item.getTitle());
        }
        if(TextUtils.isEmpty(item.getCatalogName())){
            baoniaoCategroy.setVisibility(View.GONE);
            if(lanmu_line!=null){
                lanmu_line.setVisibility(View.GONE);
            }
        }else {
            baoniaoCategroy.setText(item.getCatalogName());
          //  baoniaoCategroy.setTextColor(DefaultBgUtil.getTintColor(itemView.getContext()));
            baoniaoCategroy.setVisibility(View.VISIBLE);
            if(lanmu_line!=null){
                lanmu_line.setVisibility(View.VISIBLE);
            }
        }

        baoniaodianzanshu.setText("" + item.getFavorCount());
        baoniaodianpinglunshu.setText(TextUtils.isEmpty(item.getHitCount())?"0":item.getHitCount());
        baoLiaoCommentNum.setText("" + item.getCommentCount());
        itemView.setTag(R.id.tagSaveId, item);
        if (item.baoNiaoMediaList == null) {
            item.baoNiaoMediaList = BaoNiaoMedia.parseBaoLiaoMediaList(item);
        }
    }

    @Override
    public void onItemClick(int index, boolean isFromAdaptor, BaseRecyclerAdapter adapter) {
        BaoNiaoListItem baoNiaoListItem = itemView.getTag(R.id.tagSaveId) != null ? (BaoNiaoListItem) itemView.getTag(R.id.tagSaveId) : null;
        if(baoNiaoListItem == null){
            return;
        }
        try {
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; baoNiaoListItem.getImage() != null && i < baoNiaoListItem.getImage().size(); i++) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("info", baoNiaoListItem.getContent());
                jsonObject.put("src", baoNiaoListItem.getImage().get(i));
                jsonArray.put(jsonObject);
            }
            final ArticleItem imgArticle = new ArticleItem();
            imgArticle.setTitle(baoNiaoListItem.getCatalogName());
            imgArticle.setImage(jsonArray.toString());
            imgArticle.setType(TypeX.NEWS.PHOTOS);
            int imgIndex = index;
            if (baoNiaoListItem.getVideo() != null) {
                imgIndex -= baoNiaoListItem.getVideo().size();
            }
            NewsItemClickUtils.OpenItemNewsHandle(itemView.getContext(), TypeX.NEWS.PHOTOS, imgArticle, new CatalogItem(), imgIndex, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}